﻿using System;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    public class PackageSNChange : BaseService<PackageSNChangeInfo>
    {
        private static readonly IPackageSNChange dal = DALFactory.DataAccess.CreatePackageSNChange();

        public override void SetCurrentRepository()
        {
            CurrentRepository = dal;
        }

        public IList<PackageSNChangeInfo> GetPackageSNChangebySN(string packageSN)
        {
            return dal.GetPackageSNChangebySN(packageSN);
        }

        public IList<PackageSNChangeInfo> GetPackageSNChangebyDT(DateTime starttime, DateTime stoptime)
        {
            return dal.GetPackageSNChangebyDT(starttime, stoptime);
        }

        public string InsertRecord(PackageSNChangeInfo psci)
        {
            string result;
            PackageProduct pp = new PackageProduct();
            PackageProductInfo ppi = pp.GetPackageProductbyPackageProductSN(psci.OldPackageProductSN);
            if (string.IsNullOrEmpty(ppi.PackageProductSN))
            {
                result = "旧的成品序列号没有和腔体序列号关联过，无法替换！";
                return result;
            }
            PackageProductInfo ppi2 = pp.GetPackageProductbyPackageProductSN(psci.PackageProductSN);
            if (!string.IsNullOrEmpty(ppi2.PackageProductSN))
            {
                result = "新的成品序列号已经和其它腔体序列号关联过，无法替换！";
                return result;
            }
            try
            {
                dal.Insert(psci);
            }
            catch
            {
                result = "替换失败，请联系相关人员！";
                return result;  
            }
            result = "替换成功！";
            return result;
        }
    }
}
